package com.gemme.gemme.adapter;

import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.gemme.gemme.util.PCUtil;
import com.gemme.gemme.view.OnChangeLIstener;
import com.youth.banner.adapter.BannerAdapter;

import java.util.ArrayList;
import java.util.List;

public class ImageAdapter extends BannerAdapter<DataBean, ImageAdapter.BannerViewHolder> {

    private List<DataBean> mDatas;
    private OnChangeLIstener onChangeLIstener;

    public ImageAdapter(List<DataBean> mDatas) {
        //设置数据，也可以调用banner提供的方法,或者自己在adapter中实现
        super(mDatas);
        this.mDatas = mDatas;
    }

    //创建ViewHolder，可以用viewType这个字段来区分不同的ViewHolder
    @Override
    public BannerViewHolder onCreateHolder(ViewGroup parent, int viewType) {
        ImageView imageView = new ImageView(parent.getContext());
        //注意，必须设置为match_parent，这个是viewpager2强制要求的
        imageView.setLayoutParams(new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        return new BannerViewHolder(imageView);
    }

    @Override
    public void onBindView(BannerViewHolder holder, DataBean data, int position, int size) {
//        holder.imageView.setImageResource(data.imageRes);
        try {
            if(!TextUtils.isEmpty(data.getImageRes())){
                Glide.with(holder.itemView.getContext()).load(data.getImageRes()).into( holder.imageView);
            }else{
                Glide.with(holder.itemView.getContext()).load(data.getNativeimage()).into( holder.imageView);
            }

            holder.imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(onChangeLIstener != null)
                        onChangeLIstener.onChangeLIstener();
                }
            });
        }catch (Exception e){

        }
    }

    class BannerViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;

        public BannerViewHolder(@NonNull ImageView view) {
            super(view);
            this.imageView = view;
        }
    }

    public void setOnChangeLIstener(OnChangeLIstener onChangeLIstener){
        this.onChangeLIstener = onChangeLIstener;
    }
}
